package com.bcxj.servicemap.service;

import com.bcxj.internalcommon.constant.AmapConfigConstants;
import com.bcxj.internalcommon.dto.ResponseResult;
import com.bcxj.internalcommon.entity.DicDistrict;
import com.bcxj.servicemap.mapper.DicDistrictMapper;
import com.bcxj.servicemap.remote.DicDistrictClient;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * ClassName: DistrictService
 * Package: com.bcxj.servicemap.service
 * Description:
 *
 * @Author BCXJ
 * @Create 2024/10/11 23:21
 * @Version 1.0
 * @Since 1.0
 */
@Service
public class DistrictService {

    @Autowired
    private DicDistrictClient dicDistrictClient;

    @Autowired
    private DicDistrictMapper dicDistrictMapper;

    /**
     * 获取行政区划
     * @param keyword
     * @return
     */
    public ResponseResult getDistrict(String keyword) {

        String districtStr = dicDistrictClient.getDistrict(keyword);
//        System.out.println(districtStr);

        JSONObject jsonObject = JSONObject.fromObject(districtStr);

        if(!jsonObject.has(AmapConfigConstants.DISTRICTS_STATUS) && !jsonObject.get(AmapConfigConstants.DISTRICTS_STATUS).equals("1")){
            return ResponseResult.fail("查询失败，请检查路径");
        }

        if(!jsonObject.has(AmapConfigConstants.DISTRICTS)){
            return ResponseResult.fail("未查询到数据");
        }
        JSONArray districts = jsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);

        // 1 国家数据
        JSONObject countryJSON = districts.getJSONObject(0);
        String countryAddresscode = countryJSON.getString(AmapConfigConstants.ADDRESS_CODE);
        String countryName = countryJSON.getString(AmapConfigConstants.ADDRESS_NAME);
        Integer countryLevel = LevelParseInteger(countryJSON.getString(AmapConfigConstants.DISTRICT_LEVEL));
        DicDistrict countryDis = DicDistrict.builder().addressCode(countryAddresscode).addressName(countryName).level(countryLevel).build();
        dicDistrictMapper.insert(countryDis);


        // 省级数据定义
        JSONArray provinceJSONArray = districts.getJSONObject(0).getJSONArray(AmapConfigConstants.DISTRICTS);
        JSONObject provinceJSON = null;
        String provinceAddresscode = null;
        String provinceName = null;
        Integer provinceLevel = null;
        // 市级数据定义
        JSONArray cityJSONArray = null;
        JSONObject cityJSON = null;
        String cityAddresscode = null;
        String cityName = null;
        Integer cityLevel = null;
        // 地区数据定义
        JSONArray districtJSONArray = null;
        JSONObject districtJSON = null;
        String districtAddresscode = null;
        String districtName = null;
        Integer districtLevel = null;

        // 2 省级数据
        for (int i = 0; i < provinceJSONArray.size(); i++) {
            provinceJSON = provinceJSONArray.getJSONObject(i);
            provinceAddresscode = provinceJSON.getString(AmapConfigConstants.ADDRESS_CODE);
            provinceName = provinceJSON.getString(AmapConfigConstants.ADDRESS_NAME);
            provinceLevel = LevelParseInteger(provinceJSON.getString(AmapConfigConstants.DISTRICT_LEVEL));
            DicDistrict cityDis = DicDistrict.builder().addressCode(provinceAddresscode).addressName(provinceName).level(provinceLevel)
                    .parentAddressCode(countryAddresscode).build();
            dicDistrictMapper.insert(cityDis);
            // 市级数据
            cityJSONArray = provinceJSON.getJSONArray(AmapConfigConstants.DISTRICTS);
            for (int j = 0; j < cityJSONArray.size(); j++) {
                cityJSON = cityJSONArray.getJSONObject(j);
                cityAddresscode = cityJSON.getString(AmapConfigConstants.ADDRESS_CODE);
                cityName = cityJSON.getString(AmapConfigConstants.ADDRESS_NAME);
                cityLevel = LevelParseInteger(cityJSON.getString(AmapConfigConstants.DISTRICT_LEVEL));
                DicDistrict districtDis = DicDistrict.builder().addressCode(cityAddresscode).addressName(cityName)
                        .level(cityLevel).parentAddressCode(provinceAddresscode).build();
                dicDistrictMapper.insert(districtDis);

                // 地区数据
                districtJSONArray = cityJSON.getJSONArray(AmapConfigConstants.DISTRICTS);
                for (int k = 0; k < districtJSONArray.size(); k++) {
                    districtJSON = districtJSONArray.getJSONObject(k);
                    districtAddresscode = districtJSON.getString(AmapConfigConstants.ADDRESS_CODE);
                    districtName = districtJSON.getString(AmapConfigConstants.ADDRESS_NAME);
                    districtLevel = LevelParseInteger(districtJSON.getString(AmapConfigConstants.DISTRICT_LEVEL));
                    DicDistrict areaDis = DicDistrict.builder().addressCode(districtAddresscode).addressName(districtName)
                            .level(districtLevel).parentAddressCode(cityAddresscode).build();
                    dicDistrictMapper.insert(areaDis);
                }
            }
        }



        return ResponseResult.success();
    }


    /**
     * 将水平字符串转成水平Integer
     * @param level
     * @return
     */
    private Integer LevelParseInteger(String level) {
        if(StringUtils.isBlank(level)){
            return -1;
        }

        if(level.equals("country")){
            return 1;
        } else if(level.equals("province")){
            return 2;
        } else if(level.equals("city")){
            return 3;
        } else if(level.equals("district")){
            return 4;
        } else {
            return -1;
        }
    }

}
